package br.com.sgi.util;

import java.text.MessageFormat;
import java.util.ResourceBundle;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import javax.faces.application.FacesMessage.Severity;


public class Mensagem {

	private static ResourceBundle bundle = ResourceBundle.getBundle("MessageResources", FacesContext.getCurrentInstance().getViewRoot().getLocale());
	
	public static void errorMsg(String key) {

		String msg = bundle.getString(key);
		FacesMessage fm = new FacesMessage(FacesMessage.SEVERITY_ERROR, msg,				msg);
		FacesContext fc = FacesContext.getCurrentInstance();
		fc.addMessage("erro", fm);
	}

	public static void infoMsg(String msg) {
		//String[] messageArguments = new String[]{nomeCampo};
		msg = bundle.getString(msg);
		FacesMessage fm = new FacesMessage(FacesMessage.SEVERITY_INFO, msg, msg);
		
		FacesContext fc = FacesContext.getCurrentInstance();
	
		fc.addMessage("info", fm);
	}
	
	public static void msg(String msg, String id) {

		msg = bundle.getString(msg);
		
		
		FacesMessage fm = new FacesMessage(FacesMessage.SEVERITY_INFO, msg, msg);
		FacesContext fc = FacesContext.getCurrentInstance();
		fc.addMessage(id, fm);
	}
	
	public static String msg(String id) {

		return bundle.getString(id);
	}
	
	public static void addInfo(String key, Object... parameters) {
		addMessage(key, FacesMessage.SEVERITY_INFO, parameters);
	}

	public static void addWarn(String key, String... parameters) {
		addMessage(key, FacesMessage.SEVERITY_ERROR, parameters);
	}

	public static void addFatal(String key, Object... parameters) {
		addMessage(key, FacesMessage.SEVERITY_FATAL, parameters);
	}
	
	public static void addMessage(String key, Severity severity, Object... parameters) {
		FacesContext fc = FacesContext.getCurrentInstance();
		String message = "";
				message = setMessageParameters(bundle.getString(key), parameters);
		FacesMessage fm = new FacesMessage(severity, message, message);
		fc.addMessage(null, fm);
	}

	public static String setMessageParameters(String message, Object... parameters) {
		if (parameters != null && parameters.length > 0 && message.indexOf('{') != -1) {
			message = MessageFormat.format(message, parameters);
		}
		return message;
	}
	
	

	
	public static void msgParans(String id, Object[] parans) {

		String msg = bundle.getString(id);
		
		
		  MessageFormat formatter = new MessageFormat("");
		  

		    formatter.applyPattern(msg);
		    String output = formatter.format(parans);
		
		FacesMessage fm = new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, msg);
		FacesContext fc = FacesContext.getCurrentInstance();
		fc.addMessage(id, fm);
		
	}
	
	public static ResourceBundle getBundle() {
		FacesContext fc = FacesContext.getCurrentInstance();
		if (bundle == null) {
			bundle = ResourceBundle.getBundle("src.main.resources.MessageResources", fc.getViewRoot().getLocale());
			if(bundle == null)
				bundle = ResourceBundle.getBundle("src.main.resources.MessageResources");
		}
		return bundle;
	}
}
